/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package LoginStaff;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author maconcon
 */
public class LogServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
          
            HttpSession session = request.getSession(false);
            
            String username;
            String password;
            boolean checker;

            username = request.getParameter("admin");
            password = request.getParameter("adminpass");

            //Assuming we have a function that checks the user credentials
            checker = new LoginReg().Log(username, password);

            if(checker == true){
                String adminid  = new LoginReg().getInfo(username, "adminID");
                String admin = new LoginReg().getInfo(username, "username");
                String adminpass = new LoginReg().getInfo(username, "password");
                String firstname  = new LoginReg().getInfo(username, "FirstName");
                String middlename  = new LoginReg().getInfo(username, "Middlename");
                String lastname  = new LoginReg().getInfo(username, "Lastname");
                String usertype = new LoginReg().getInfo(username, "userType");
               
                
                
               
                //new LoginManager().updateLastLog(username);
                
                session.setAttribute("adminid", adminid);                     
                session.setAttribute("username",admin);
                session.setAttribute("password",adminpass);        
                session.setAttribute("firstname",firstname);             
                session.setAttribute("middlename",middlename);             
                session.setAttribute("lastname",lastname);   
                session.setAttribute("usertype", usertype);
                
              
                if(usertype.equals("2"))
                    response.sendRedirect("administrator.jsp");
                else if(usertype.equals("1"))
                    response.sendRedirect("login1234foobar.jsp");
                else if(usertype.equals("3"))
                    response.sendRedirect("login4567foobar.jsp");
            }
            else
             {
              
              response.sendRedirect("administrator.jsp");
            }
            
            
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
